<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibConfig;
use app\common\model\hfztcy\LibVideoCategory;
use common\Common;


/**
 * 视频栏目
 * @name VideoCategory.php
 * @package hfztcy
 * @videoCategory controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class VideoCategory extends AdminController
{
    protected $model;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibVideoCategory();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 列表页
     */
    function videoCategoryList()
    {
        $this->getSetMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('VideoCategory/videoCategoryList/videoCategoryList');
    }

    /**
     * 添加页面
     */
    function addVideoCategory()
    {
        $this->getMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "添加页面", 1, 'view');
        return $this->fetch('VideoCategory/addVideoCategory/addVideoCategory');
    }

    /**
     * 添加
     */
    function insertVideoCategory()
    {
        $videoCategoryInfo = $this->getArgsList($this, array('title', 'video_url'), false);
        $videoCategoryInfo['add_time'] = Common::getTime();
        $result = $this->model->addVideoCategory($videoCategoryInfo);
        if ($result['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
            die;
        }
        $this->log(__CLASS__, __FUNCTION__, "添加", 0, 'add');
        echo json_encode($result);
    }

    /**
     * 编辑页面
     */
    function editVideoCategory()
    {
        $this->getMenu($this);
        $videoCategoryInfo = $this->model->findVideoCategory(['id' => input("id")]);
        $videoCategoryInfo['data']['video_url'] = str_replace('upload/','upload/sliceVideo/',$videoCategoryInfo['data']['video_url']);
        $this->assign('videoCategoryInfo', $videoCategoryInfo['data']);
        $configModel = new LibConfig();
        $findConfigRes = $configModel->findConfig(['item_key' => 'category_posters_img']);
        $this->assign('category_posters_img',$findConfigRes['data']['item_value']);
        $this->log(__CLASS__, __FUNCTION__, "编辑页面", 1, 'view');
        return $this->fetch('VideoCategory/editVideoCategory/editVideoCategory');
    }

    /**
     * 更新
     */
    function updateVideoCategory()
    {
        $args = $this->getArgsList($this, array('id', 'title', 'video_url'), false);
        $id = $args['id'];
        $args['add_time'] = Common::getTime();
        $vidoe_url = str_replace('upload/sliceVideo/','upload/',$args['video_url']);
        $args['video_url'] = $vidoe_url;
        $result = $this->model->updateVideoCategory(array("id" => $id), $args);
        if ($result['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
            die;
        }
        $videoCategoryInfo = $this->model->findVideoCategory(['id' => $id]);
        if ($vidoe_url != $videoCategoryInfo['data']['video_url']) {
            if (file_exists(ROOT_PATH.'public/'.$vidoe_url)) {
                unlink(ROOT_PATH.'public/'.$vidoe_url);
            }
        }
        $this->log(__CLASS__, __FUNCTION__, "更新", 0, 'update');
        echo json_encode($result);
    }

    /**
     * 删除
     */
    function deleteVideoCategory()
    {
        $id = input("id");
        $videoCategoryRes = $this->model->findVideoCategory(['id' => $id]);
        if ($videoCategoryRes['errorCode'] == 0) {
            $result = $this->model->deleteVideoCategory($id);
            $vidoe_url = str_replace('upload/','upload/sliceVideo/',$videoCategoryRes['data']['video_url']);
            if (file_exists(ROOT_PATH.'public/'.$vidoe_url)) {
                unlink(ROOT_PATH.'public/'.$vidoe_url);
            }
            echo json_encode($result);
            die();
        }
        echo json_encode(Common::errorArray(1, '不存在', false));
        die();
    }

    /**
     * Info: 删除栏目视频
     * Date: 2023/2/16 13:52
     */
    function deleteVideoUrl ()
    {
        $findVcrRes = $this->model->findVideoCategory(['id' => input('id')]);
        $video_url = $findVcrRes['data']['video_url'];
        $video_url = str_replace('upload/','upload/sliceVideo/',$video_url);
        if (file_exists(ROOT_PATH.'public/'.$video_url)) {
            unlink(ROOT_PATH.'public/'.$video_url);
            $this->model->updateVideoCategory(['id' => $findVcrRes['data']['id']], ['video_url' => '']);
        }
        echo json_encode(Common::errorArray(0, '删除成功', true));
        die;
    }

    /**
     * 分页查询
     */
    function pagingVideoCategory()
    {
        $args = $this->getArgsList($this, ['title']);
        $page = $this->getPageInfo();
        $where = [];
        if (!empty($args['title'])) $where[] = "`title` like '%" . $args['title'] . "%'";
        $result = $this->model->pagingVideoCategory($page, implode(' AND ', $where));
        foreach ($result['data']['dataList'] as &$item) {
            $item['video_url'] = ROOT_URL.'/'.str_replace('upload/','upload/sliceVideo/',$item['video_url']);
        }
        echo json_encode($result);
    }
}

?>